package cn.yotta.user.web.autoconfiguration.provider.impl;

import cn.yotta.common.api.model.Response;
import cn.yotta.user.api.params.user.UserFindByTokenRequest;
import cn.yotta.user.api.user.UserReadApi;
import cn.yotta.user.api.vo.UserVo;
import cn.yotta.user.web.autoconfiguration.exception.UserLoginException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wyaoyao
 * @date 2023/3/10 18:13
 * 适配yotta-user的远程调用实现(feign实现)
 */
@Slf4j
public class YottaRemoteUserReadFeignProvider extends AbstractYottaUserReadProvider {

    private final UserReadApi userReadApi;

    public YottaRemoteUserReadFeignProvider(UserReadApi userReadApi) {
        this.userReadApi = userReadApi;
    }

    @Override
    protected UserVo doFindUserByToken(UserFindByTokenRequest findByTokenRequest) {
        final Response<UserVo> byToken = userReadApi.findByToken(findByTokenRequest);
        return byToken.orElseThrow(() -> new UserLoginException(findByTokenRequest.getToken() + ":" + byToken.getError()));
    }

}
